package cn.chiship.bs.api.biz.controller;


import cn.chiship.bs.api.biz.entity.*;
import cn.chiship.bs.api.biz.pojo.dto.MemberUserDto;
import cn.chiship.bs.api.biz.pojo.dto.OrderCreateDto;
import cn.chiship.bs.api.biz.pojo.dto.ProductCategoryDto;
import cn.chiship.bs.api.biz.service.BaseService;
import cn.chiship.bs.api.biz.service.MemberUserService;
import cn.chiship.bs.api.biz.service.OrderHeaderService;
import cn.chiship.bs.api.biz.service.UserCacheService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.core.util.ip.IpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 订单控制层
 * 2023/3/26
 *
 * @author lijian
 */
@RestController
@RequestMapping("/member")
@Api(tags = "用户")
public class MemberUserController extends BaseController<MemberUser, MemberUserExample> {

    private static final Logger LOGGER = LoggerFactory.getLogger(MemberUserController.class);

    @Resource
    private MemberUserService memberUserService;

    @Override
    public BaseService getService() {
        return memberUserService;
    }

    @ApiOperation(value = "用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序方式", defaultValue = "-gmtModified", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataType = "String", paramType = "query"),

    })
    @GetMapping(value = "/page")
    public ResponseEntity<BaseResult> page(
            @RequestParam(required = false, defaultValue = "", value = "keyword") String keyword,
            @RequestParam(required = false, defaultValue = "", value = "orderStatus") Byte orderStatus) {
        MemberUserExample memberUserExample = new MemberUserExample();
        //创造条件
        MemberUserExample.Criteria memberUserExampleCriteria = memberUserExample.createCriteria();
        memberUserExampleCriteria.andIsDeletedEqualTo(BaseConstants.NO);
        if (!StringUtil.isNullOrEmpty(keyword)) {
            memberUserExampleCriteria.andRealNameLike(keyword + "%");
        }
        return super.responseEntity(BaseResult.ok(super.page(memberUserExample)));
    }

    @ApiOperation(value = "完善信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会员id", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "perfect/{id}")
    public ResponseEntity<BaseResult> update(
            HttpServletRequest request,
            @PathVariable("id") String id,
            @RequestBody @Valid MemberUserDto memberUserDto) {
        MemberUser memberUser = new MemberUser();
        BeanUtils.copyProperties(memberUserDto, memberUser);
        super.update(id, memberUser);
        memberUser=memberUserService.selectByPrimaryKey(id);
        return super.responseEntity(memberUserService.appletLogin(memberUser.getWeixin1OpenId(), IpUtils.getIpAddr(request)));
    }


}